//1.链表 - 两数相加
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* newhead = new ListNode(-1), *tail = newhead;
        ListNode* cur1 = l1, *cur2 = l2;
        int n = 0; //进位
        while(cur1 || cur2 || n)
        {
            int sum = n;
            if(cur1)
            {
                sum += cur1->val;
                cur1 = cur1->next;
            }
            if(cur2)
            {
                sum += cur2->val;
                cur2 = cur2->next;
            }
            ListNode* node = new ListNode(sum % 10);
            tail->next = node;
            tail = tail->next;
            n = sum / 10;
        }  
        return newhead->next;
    }
};

//2.链表 - 两两交换链表中的节点
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* newHead = new ListNode(-1);
        if(head == nullptr || head->next == nullptr) return head;
        ListNode* prev = newHead, *cur = head, *next = cur->next, *nnext = next->next;
        while(cur && next)
        {
            //两两交换
            prev->next = next;
            next->next = cur;
            cur->next = nnext;
            //循环迭代
            prev = cur;
            cur = nnext;
            if(cur) next = cur->next;
            if(next) nnext = next->next;
        }
        cur = newHead->next;
        delete newHead;
        return cur;
    }
};